package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.Message;
import com.rongke.model.MsgUser;
import com.rongke.model.Muser;
import com.rongke.model.UserCondition;
import com.rongke.service.MessageService;
import com.rongke.service.MsgUserService;

import com.rongke.service.MuserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @MsgUserController
 * @用户消息Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/msgUser")
@Transactional
@CrossOrigin
public class MsgUserController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private MsgUserService msgUserService;
    @Autowired
    private MuserService muserService;
    @Autowired
    private MessageService messageService;

    /**
     * @添加用户消息
     * @param msgUser
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addMsgUser(@RequestBody MsgUser msgUser){
        log.debug("添加用户消息");
        msgUser.setGmtDatetime(new Date());
        msgUserService.insert(msgUser);
        return JsonResp.ok(msgUser);
    }

    /**
     * @修改用户消息
     * @param msgUser
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateMsgUser(@RequestBody MsgUser msgUser){
        log.debug("修改用户消息");
        msgUserService.updateById(msgUser);
        return JsonResp.ok(msgUser);
    }
    /**
     * @根据id查找用户消息
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectMsgUser(Long id){
        log.debug("查找用户消息");
        MsgUser msgUser = msgUserService.selectById(id);
        return JsonResp.ok(msgUser);
    }

    /**
     * @根据条件查找用户消息
     * @param msgUser
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectMsgUserByCondition(@RequestBody MsgUser msgUser){
        log.debug("根据条件查找用户消息");
        if(msgUser==null)
            return JsonResp.toFail("参数不能为空");
        List<MsgUser> msgUserList=msgUserService.selectByCondition(msgUser);
        return JsonResp.ok(msgUserList);
    }


    /**
     * 根据条件指定对象发送消息
     * @param condition
     * @param msgId
     * @return
     */
    @RequestMapping(value = "/publishMsg4PartUsers",method = RequestMethod.POST)
    public JsonResp publishMsg4PartUsers(@RequestBody UserCondition condition, Long msgId){
        log.debug("根据条件指定对象发送消息");
        if(null== msgId)
            return JsonResp.toFail("参数不能为空");
        //获取推送用户id列表
        EntityWrapper<Muser> ewm = new EntityWrapper<>(condition.getMuser());
        ewm.isNotNull("user_name");
        ewm.setSqlSelect("id");
        if(condition.getMinAge()!=null){
            //大于最小年龄
            ewm.ge("(year(now())-year(birthday))",condition.getMinAge());
        }
        if(condition.getMaxAge()!=null){
            //小于最大年龄
            ewm.le("(year(now())-year(birthday))",condition.getMaxAge());
        }
        List<Object> ids = muserService.selectObjs(ewm);
        if(null!= ids &&!ids.isEmpty()){
            Map map = new HashMap();
            map.put("msgId", msgId);
            map.put("userIds", ids);
//        long start=System.currentTimeMillis();
            msgUserService.insertBatchMsg(map);
//        long end=System.currentTimeMillis();
//        System.out.println("插入消息条数:"+ids.size());
//        System.out.println("执行时间"+(start-end)+"ms");
        }
        Message msg=new Message(msgId);
        msg.setStatus(2);//设置消息状态为已发布
        msg.setUptDatetime(new Date());
        messageService.updateById(msg);
        return JsonResp.ok("发布消息成功");
    }

}
